import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
  Repository,
  FindConditions,
  FindOneOptions,
  FindManyOptions,
} from 'typeorm';
import { Service } from './entities/service.entity';

// 具有单一方法的基本服务
@Injectable()
export class ServiceService {
  constructor(
    @InjectRepository(Service)
    private readonly serviceRepository: Repository<Service>,
  ) {}

  // 查询所有的服务使用
  findAndCount(query) {
    const getList = this.serviceRepository
      .createQueryBuilder()
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    const list = getList;
    return list;
  }

  // 账号状态条件查询
  findByAccountStatus(accountStatus, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.accountStatus =:accountStatus', { accountStatus })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 负责销售查询
  findBySellId(sellId, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.sellId =:sellId', { sellId })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 日均流量 筛选
  findByFlowDayAverage(flowDayAverage, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.flowDayAverage LIKE :flowDayAverage', {
        flowDayAverage: `%${flowDayAverage}%`,
      })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 已用存储 筛选
  findByStorageTotal(storageTotal, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.storageTotal LIKE :storageTotal', {
        storageTotal: `%${storageTotal}%`,
      })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 代金券余额 筛选
  findByCouponRemain(couponRemain, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.couponRemain LIKE :couponRemain', {
        couponRemain: `%${couponRemain}%`,
      })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 账号搜索
  findByAccountName(accountName, query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.accountName LIKE :accountName')
      .setParameters({ accountName: '%' + accountName + '%' })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }

  // 负责日期筛选
  findByTime(query) {
    const service = this.serviceRepository
      .createQueryBuilder('s')
      .where('s.updatedAt BETWEEN :startTime AND :endTime', {
        startTime: query.startTime,
        endTime: query.endTime,
      })
      .skip(query.size * (query.page - 1))
      .take(query.size)
      .getMany();
    return service;
  }
}
